﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Core
{
    public static partial class problem
    {
        public static dynamic deleteProblem(int pid)
        {
            var sql = new Sql.SqlDataContext();
            var p = sql.Problem.FirstOrDefault(row => row.pid == pid);
            if (p == null) return new
            {
                success = false,
                msg = "题目不存在"
            };

            // Delete Status
            var sts = sql.Status.Where(row => row.pid == pid);
            foreach (var st in sts)
            {
                if (st.status < 3 || st.god) continue;
                // Update US
                var us = sql.UserStatistics.FirstOrDefault(row => row.username == st.username);
                if (us != null) user.deleteUserStatistics(us, st.status);
            }
            sql.Status.DeleteAllOnSubmit(sts);

            // Delete PS
            var ps = sql.ProblemStatistics.FirstOrDefault(row => row.pid == pid);
            if (ps != null) sql.ProblemStatistics.DeleteOnSubmit(ps);

            // Delete Solve
            sql.Solve.DeleteAllOnSubmit(sql.Solve.Where(row => row.pid == pid));

            // Delete Discuss
            sql.Discuss.DeleteAllOnSubmit(sql.Discuss.Where(row => row.pid == pid));

            // Delete Problem
            sql.Problem.DeleteOnSubmit(p);
            sql.SubmitChanges();
            return new
            {
                success = true
            };
        }

        internal static void deleteProblemStatistics(Sql.ProblemStatistics ps, int status)
        {
            if (status >= 3) --ps.submit;
            switch (status)
            {
                case 3:
                    --ps.accept;
                    break;
                case 4:
                    --ps.pe;
                    break;
                case 5:
                    --ps.tle;
                    break;
                case 6:
                    --ps.mle;
                    break;
                case 7:
                    --ps.ole;
                    break;
                case 8:
                    --ps.wa;
                    break;
                case 9:
                    --ps.re;
                    break;
                case 10:
                    --ps.ce;
                    break;
                case 11:
                    --ps.se;
                    break;
            }
        }
    }
}
